package com.fx.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fx.entity.CstInfo;
import com.fx.mapper.CstInfoMapper;
import com.fx.service.CstInfoService;
import com.fx.util.IdWorker;
import com.fx.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class CstInfoServiceImpl implements CstInfoService {
    @Autowired
    private CstInfoMapper cstInfoMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public CstInfo getById(String flId) {
        return cstInfoMapper.getById(flId);
    }

    @Override
    public List<CstInfo> getList(Map<String, Object> map) {
        return cstInfoMapper.queryList(map);
    }

    @Override
    public int getCount(Map<String, Object> map) {
        return cstInfoMapper.queryCount(map);
    }

    @Override
    public int save(CstInfo cstInfo) {
        cstInfo.setFlId(String.valueOf(idWorker.nextId()));
        String pwd = cstInfo.getFlLoginPwd();
        if(pwd != null && !"".equals(pwd)) {
            pwd = MD5Util.digestHex(pwd);
            cstInfo.setFlLoginPwd(pwd);
        }
        cstInfo.setFlRegisteredTime(DateUtil.now());
        return cstInfoMapper.save(cstInfo);
    }

    @Override
    public int updateById(CstInfo cstInfo) {
        String pwd = cstInfo.getFlLoginPwd();
        if(pwd != null && !"".equals(pwd)) {
            pwd = MD5Util.digestHex(pwd);
            cstInfo.setFlLoginPwd(pwd);
        }
        return cstInfoMapper.updateById(cstInfo);
    }

    @Override
    public int remove(String flId) {
        return cstInfoMapper.remove(flId);
    }

    @Override
    public int batchRemove(String[] flIds) {
        return cstInfoMapper.batchRemove(flIds);
    }

    @Override
    public CstInfo getByLoginName(String userName) {
        return cstInfoMapper.queryByName(userName);
    }

}
